package com.qf.springbootproject.order.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.springbootproject.order.entity.Orders;
import com.qf.springbootproject.order.service.OrdersService;
import com.qf.springbootproject.utils.ResultVo;
import com.qf.springbootproject.wechat.WxpayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 订单  前端控制器
 * </p>
 *
 * @author weq
 * @since 2023-02-17
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    /**
     * 下单接口
     *
     * @param cids
     * @param orders
     * @return
     */
    @PostMapping("/add")
    public ResultVo add(Integer[] cids, @RequestBody Orders orders) throws Exception {
        Map<String, Object> data = ordersService.add(cids, orders);
        return ResultVo.ok(data);
    }

    /**
     * 订单支付接口
     *
     * @param oid
     * @return
     */
    @GetMapping("/status/{oid}")
    public ResultVo status(@PathVariable("oid") String oid) throws Exception {
        //查询订单支付状态
        String queryOrder = WxpayUtils.queryOrder(oid);
        //如果订单支付成功，返回字符串 2
        if (queryOrder.equals("SUCCESS")) {
            ordersService.update(new UpdateWrapper<Orders>().set("status", 2));
            return ResultVo.ok(2);
        }
        return ResultVo.ok(1);
    }
}













